# -*- coding: utf-8 -*-
# @Author  : 廖善武
# @Email   : liaosw18878@hundsum.com
# @Time    : 2018/6/20 14:56
# @File    : OrkPublic.py
from BladeFerm.Lib.ClassSelenium import ClassSelenium
from selenium.webdriver.common.keys import Keys
import time
from BladeFerm.Lib.Log import logger
from BladeFerm.Lib.Common import setFail, config

def login(username, password):
    # 登入
    main_page = config.get('ScriptsDir', 'mainpage_url')
    browser_type = config.get('ScriptsDir', 'browser_type')
    global driver
    driver = ClassSelenium(main_page, browser_type)
    element = driver.getElementByAttribute('xpath://input[@name="username"]')
    if not element:
        driver.close()
        exit()
    else:
        element.send_keys(username)
    element = driver.getElementByAttribute('id:user_password')
    if not element:
        driver.close()
        exit()
    else:
        element.send_keys(password)
    element = driver.getElementByAttribute('id:login_submit')
    if not element:
        driver.close()
        exit()
    else:
        element.click()
    return driver

# 查询界面的公共方法
def getMenu(menuName, elementObj):
    # 定位到具体菜单
    element = driver.getElementByAttribute('xpath://*[@id="dijit_form_TextBox_0"]')
    if not element:
        driver.close()
        exit()
    else:
        element.send_keys(menuName)
        time.sleep(1)
        element.send_keys(Keys.ENTER)
        time.sleep(2)
        logger.info('测试开始：定位到【%s】菜单' %menuName)
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        time.sleep(1)
        element.click()

def getDate(date, elementObj):
    # 获取日期
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.clear()
        element.send_keys(str(date))
        logger.info('输入日期为:%s' %date)

def getBeginDate(beginDate, elementObj):
    # 获取起始日期
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.clear()
        element.send_keys(str(beginDate))
        logger.info('输入起始日期:%s' %beginDate)

def getEndDate(endDate, elementObj):
    # 获取结束日期
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.clear()
        element.send_keys(str(endDate))
        logger.info('输入结束日期:%s' %endDate)

def write(msg, elementObj):
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.clear()
        element.send_keys(msg)
        logger.info('输入信息为：%s' % msg)

def selectMethod(select, clickElement, controlElement):
    element = driver.getElementByAttribute(clickElement)
    if not element:
        driver.close()
        exit()
    else:
        element.click()
    elementList = driver.getElementByAttribute(controlElement, getAll=True)
    if not elementList:
        driver.close()
        exit()
    else:
        for element in elementList:
            if element.getText() == select:
                time.sleep(0.2)
                element.click()
                logger.info('选中的内容为:%s' %select)
                break

def search(elementObj):
    # 点击查询
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.click()
        time.sleep(4)
        logger.info('测试【查询】功能')
        logger.info('点击【查询】')

def export(elementObj):
    # 点击导出
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
        exit()
    else:
        element.click()
        time.sleep(8)
        logger.info('测试【导出】功能')
        logger.info('点击【导出】')

def select(msg, selectElement, searchElement):
    # 下拉框选择内容
    element = driver.getElementByAttribute(selectElement)
    if not element:
        driver.close()
        exit()
    else:
        element.click()
    element = driver.getElementByAttribute(searchElement)
    if not element:
        driver.close()
        exit()
    else:
        element.send_keys(msg)
        element.send_keys(Keys.ENTER)
        logger.info('选择的信息为：%s' %msg)

def check(rightMsg, elementObj):
    # 设置断言
    checkMsg = driver.getElementByAttribute(elementObj)
    if not checkMsg:
        driver.close()
        exit()
    else:
        checkMsg = checkMsg.getText()
        logger.info('期望信息为：%s' % rightMsg)
        logger.info('断言信息为：%s' % checkMsg)
        if rightMsg == str(checkMsg):
            msg = "断言成功"
            logger.info('断言结果为：%s' % msg)
        else:
            msg = "断言失败！"
            setFail("断言失败！")
            logger.error('断言结果为：%s' % msg)
            driver.close()
            exit()

def close(menuName):
    # 关闭测试窗口
    driver.close()
    logger.info('关闭【%s】测试窗口 '% menuName)